<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="value" />
      <button @click="add">添加</button>

      <ul>
        <li v-for="(item, index) in list" :key="item.id">
          <input type="checkbox" v-model="item.show" />
          {{ item.name }}
          <button v-show="item.show">删除</button>
          <button v-show="item.show" @click="up(index)">↑</button>
        </li>
      </ul>

      <button @click="piliang">批量删除</button>
    </div>

    <script src="../Vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          value: "",
          list: [
            {
              name: "zhangsan",
              id: 1,
              show: false,
            },
            {
              name: "lisi",
              id: 2,
              show: false,
            },
            {
              name: "wangwu",
              id: 3,
              show: false,
            },
          ],
        },
        methods: {
          add() {
            this.list.push({
              name: this.value,
              id: new Date().getTime(),
              show: false,
            });
          },
          up(i) {
            if (i) {
              [this.list[i - 1], this.list[i]] = [
                this.list[i],
                this.list[i - 1],
              ];
              this.list = [...this.list];
            }
          },
          piliang() {
            const arr = this.list.filter((item) => item.show);
            console.log(arr);
          },
        },
      });
    </script>
  </body>
</html>
